import org.kwis.msp.lcdui.*;

public class ScreenManager extends Card{

	private GameManager manager;

	public final static int RED=0x00FF0000;
    public final static int GREEN=0x0000FF00;
    public final static int BLUE=0x000000FF;
    public final static int WHITE=0x00FFFFFF;
    public final static int BLACK=0x00000000;
    public final static int GREY=0X00808080;
    public final static int YELLOW=0X00808080;


	private boolean titleScreen = true;

	public ScreenManager(){
		
	}// ScreenManager Constructor

//-------------------  setGameManager()  -----------------
	public void setGameManager(GameManager manager){
		
		this.manager=manager;

	}

		
		
//------------------  paint()  -----------------------
		public void paint(Graphics g){

			if(titleScreen){
				paintTitleScreen(g);
			}//if
			else{
				paintBattleField(g);
			}

		}//paint()


//------------------  paintTitleScreen()  ------------------

		public void paintTitleScreen(Graphics g){

			Image imgTitle= null;
			Image imgTitleLatter = null;


			g.setColor(GREY);
			g.fillRect(0, 0, getWidth(), getHeight());

			try{
				imgTitle= Image.createImage("/InvaderMain.bmp");
				imgTitleLatter= Image.createImage("/InvaderLatter.png");
			}catch(Exception e){}

			g.drawImage(imgTitle , getWidth()/2, 60, Graphics.TOP|Graphics.HCENTER);
			g.drawImage(imgTitleLatter,getWidth()/2, 20, Graphics.TOP|Graphics.HCENTER);
		
		} //paintTieleScreen()


//-------------  paintBattleField()  ---------------------

		public void paintBattleField(Graphics g){

			GameMemberGroup memGroup ;
			GameMember member;

			g.setColor(BLACK);
			g.fillRect(0, 0, getWidth(), getHeight());

			synchronized(manager){
				memGroup = manager.getAliens();

				for (int i=0; i<memGroup.size(); i++) 
				{
					member = (GameMember)memGroup.elementAt(i);
						
					member.paint(g);
				}

				memGroup = manager.getPlayerGroup();

				for (int i=0; i<memGroup.size(); i++) {
		
					member = (GameMember)memGroup.elementAt(i);
						
					member.paint(g);
				}

				member=manager.getUFO();
				member.paint(g);

				memGroup = manager.getMissilesUFO();

				for (int i=0; i<memGroup.size(); i++) 
				{
					member = (GameMember)memGroup.elementAt(i);
						
					member.paint(g);
				}


			}//synchronized

		
		}//paintBattleField()


//---------------   keyNotify()  ------------------------

		protected boolean keyNotify(int type, int key){
			if(titleScreen){
				titleScreen = false;
				repaint();
				manager.start();
				return true;
			}//if

			int action = Display.getGameAction(key);

			if(type==EventQueue.KEY_PRESSED)
			{
				if(manager != null)
					manager.processAction(action);
			}

			return true;

		} // keyNotify()


}//Class ScreenManager